// index.js
// 获取应用实例
const app = getApp()

Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseGetUserProfile: false,
        canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
        imageUrl: "https://img01.yzcdn.cn/vant/ipad.jpeg",
        samples: ''
    },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onShow() {
        this.getTabBar().init();
        wx.request({
            // url: `http://127.0.0.1:5000/getSample`,
            url: app.api + "getSample",
            method: 'get',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: (res) => {
                console.log(res)
                this.setData({
                    samples: res.data
                })
            },
            fail(res) {
                console.log(res)
                wx.showToast({
                    title: '获取样品失败',
                    icon: 'error',
                    duration: 2000
                })
            }
        })
    },
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
            desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                console.log(res)
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        })
    },
    getUserInfo(e) {
        // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
        console.log(e)
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    QRCode() {
        wx.scanCode({
            success(res) {
                // app.globalData.id=res.result;
                // console.log(app.globalData.id)
                wx.navigateTo({
                    url: '/pages/record/record?id=' + res.result,
                })
            }
        })
    },
    Update(event) {
        let data = ''
        for (var i in this.data.samples) {
            if ((this.data.samples[i]['id'] == event.currentTarget.dataset.id)) {
                data = this.data.samples[i]
            }
        }
        console.log(data)
        data = JSON.stringify(data)
        wx.navigateTo({
            url: '/pages/update/update?data=' + data,
        })
    },
    Receive(event) {
        let data = ''
        for (var i in this.data.samples) {
            if ((this.data.samples[i]['id'] == event.currentTarget.dataset.id)) {
                data = this.data.samples[i]
            }
        }
        data = JSON.stringify(data)
        wx.navigateTo({
            url: '/pages/receive/receive?data=' + data,
        })
    },
    getNotComplete() {
        let data = []
        for (var i in this.data.samples) {
            console.log()
            if (this.data.samples[i]['complete'] == false) {
                data.push(this.data.samples[i])
            }
        }
        if (data == '') {
            console.log("样品数据为空")
            wx.showToast({
                title: '样品数据为空',
                icon: 'error',
                duration: 2000
            })
            return;
        }
        let csv = ''
        for (var key in data[0]) {
            csv += key + ','
        }
        csv += '\n'
        for (var i in data) {
            // console.log(userdata[i].valueOf())
            for (var key in data[i]) {
                csv += data[i][key] + ','
            }
            csv += '\n'
        }
        console.log(csv)
        //将csv写入样本数据.csv文件
        // 引入fs模块
        const fs = wx.getFileSystemManager()
        fs.writeFile({
            filePath: `${wx.env.USER_DATA_PATH}/待接收样本数据.csv`,
            data: csv,
            encoding: 'utf8',
            success(res) {
                console.log(res)
                wx.shareFileMessage({
                    filePath: `${wx.env.USER_DATA_PATH}/待接收样本数据.csv`,
                    success(res) {
                        console.log(res)
                    },
                    fail(res) {
                        console.log(res)
                    }
                })
            },
            fail(res) {
                console.error(res)
            }
        })
    },
    getComplete() {
        let data = []
        for (var i in this.data.samples) {
            console.log()
            if (this.data.samples[i]['complete'] == true) {
                data.push(this.data.samples[i])
            }
        }
        if (data == '') {
            console.log("样品数据为空")
            wx.showToast({
                title: '样品数据为空',
                icon: 'error',
                duration: 2000
            })
            return;
        }
        let csv = ''
        for (var key in data[0]) {
            csv += key + ','
        }
        csv += '\n'
        for (var i in data) {
            // console.log(userdata[i].valueOf())
            for (var key in data[i]) {
                csv += data[i][key] + ','
            }
            csv += '\n'
        }
        console.log(csv)
        //将csv写入样本数据.csv文件
        // 引入fs模块
        const fs = wx.getFileSystemManager()
        fs.writeFile({
            filePath: `${wx.env.USER_DATA_PATH}/已接收样本数据.csv`,
            data: csv,
            encoding: 'utf8',
            success(res) {
                console.log(res)
                wx.shareFileMessage({
                    filePath: `${wx.env.USER_DATA_PATH}/已接收样本数据.csv`,
                    success(res) {
                        console.log(res)
                    },
                    fail(res) {
                        console.log(res)
                    }
                })
            },
            fail(res) {
                console.error(res)
            }
        })
    },
    getAll() {
        let data = this.data.samples
        if (data == '') {
            console.log("样品数据为空")
            wx.showToast({
                title: '样品数据为空',
                icon: 'error',
                duration: 2000
            })
            return;
        }
        let csv = ''
        for (var key in data[0]) {
            csv += key + ','
        }
        csv += '\n'
        for (var i in data) {
            for (var key in data[i]) {
                csv += data[i][key] + ','
            }
            csv += '\n'
        }
        console.log(csv)
        //将csv写入样本数据.csv文件
        // 引入fs模块
        const fs = wx.getFileSystemManager()
        fs.writeFile({
            filePath: `${wx.env.USER_DATA_PATH}/全部样本数据.csv`,
            data: csv,
            encoding: 'utf8',
            success(res) {
                console.log(res)
                wx.shareFileMessage({
                    filePath: `${wx.env.USER_DATA_PATH}/全部样本数据.csv`,
                    success(res) {
                        console.log(res)
                    },
                    fail(res) {
                        console.log(res)
                    }
                })
            },
            fail(res) {
                console.error(res)
            }
        })
    }
})